import time
import sys
from alg import quick_sort

def test_dataset(filename):
    try:
        with open(filename, 'r') as f:
            original = list(map(int, f.read().split()))
    except Exception as e:
        return False, None, str(e)
    
    expected = sorted(original)
    start_time = time.perf_counter()
    try:
        result = quick_sort(original)
    except Exception as e:
        return False, None, str(e)
    duration = time.perf_counter() - start_time
    
    is_correct = result == expected
    return is_correct, duration, None

def main():
    test_files = [
        "random_1000.txt",
        "sorted_1000.txt",
        "reverse_1000.txt",
        "uniform_1000.txt",
        "edge_cases.txt"
    ]
    
    all_passed = True
    print("=== Sorting Algorithm Tests ===")
    
    for file in test_files:
        print(f"\nTesting {file}:")
        result, duration, error = test_dataset(file)
        
        if error:
            print(f"ERROR: {error}")
            all_passed = False
            continue
            
        status = "PASSED" if result else "FAILED"
        print(f"Result: {status} | Time: {duration:.6f}s")
        if not result:
            all_passed = False
    
    print("\n=== Test Summary ===")
    print("All tests passed!" if all_passed else "Some tests failed!")

if __name__ == "__main__":
    main()